package com.lidong.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.lidong.entity.Dept;

/**
 * @Description : 
 * @Author : lidong
 * @Date : 2017年11月2日
 */
@RestController
@RequestMapping("/consumer/dept")
public class ConsumerDeptController {

	private static final String DEPT_GET_URL = "http://server.com:8001/dept/get/"; 
	private static final String DEPT_ADD_URL = "http://server.com:8001/dept/add/"; 
	private static final String DEPT_LIST_URL = "http://server.com:8001/dept/list/"; 
	private static final String DEPT_MODIFY_URL = "http://server.com:8001/dept/modify/";
	
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private HttpHeaders httpHeaders;
	
	@RequestMapping(value = "/get/{deptNo}", method = RequestMethod.GET)
	public Dept get(@PathVariable String deptNo) {
		
		Dept dept = restTemplate.exchange(DEPT_GET_URL + deptNo, HttpMethod.GET, new HttpEntity<Object>(httpHeaders), Dept.class).getBody();
		
		// Dept dept = restTemplate.getForObject(DEPT_GET_URL + deptNo, Dept.class);
		return dept;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	public List<Dept> get() {
		List<Dept> depts = restTemplate.exchange(DEPT_LIST_URL, HttpMethod.GET, new HttpEntity<Object>(httpHeaders), List.class).getBody();
		// List<Dept> depts = restTemplate.getForObject(DEPT_LIST_URL, List.class);
		return depts;
	}
	
	@RequestMapping("/add")
	public Boolean add(@RequestBody Dept dept) {
		return restTemplate.exchange(DEPT_ADD_URL, HttpMethod.POST, new HttpEntity<Object>(dept, httpHeaders), Boolean.class).getBody();
		// return restTemplate.postForObject(DEPT_ADD_URL, dept, Boolean.class);
	}
	
	@RequestMapping("/modify/{deptNo}")
	public Boolean modify(@PathVariable long deptNo, @RequestBody Dept newDept) {
		return restTemplate.exchange(DEPT_MODIFY_URL + deptNo, HttpMethod.POST, new HttpEntity<Object>(newDept, httpHeaders), Boolean.class).getBody();
		// return restTemplate.postForObject(DEPT_MODIFY_URL + deptNo, newDept, Boolean.class);
	}
	
}
